#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

#include "global_def.h"
#include "navigation.h"
#include "toptitle.h"
#include "minitrain.h"
//TCMS
#include "TCMS/Operation/pageoperation.h"
#include "TCMS/Operation/pagespdlmtreason.h"
#include "TCMS/Operation/pageebtrigreason.h"
#include "TCMS/Operation/pagebypass.h"
#include "TCMS/Operation/pageselftest.h"
#include "TCMS/Operation/pagetractionblock.h"
#include "TCMS/Operation/pagehelpoperation.h"
#include "TCMS/Vehicle/pagevehiclesidenavi.h"
#include "TCMS/Vehicle/pagetraction.h"
#include "TCMS/Vehicle/pagebrake.h"
#include "TCMS/Vehicle/pageaux.h"
#include "TCMS/Vehicle/pagebattery.h"
#include "TCMS/Vehicle/pagefire.h"
#include "TCMS/Vehicle/pagepecu.h"
#include "TCMS/HVAC/pagehvac.h"
#include "TCMS/PISSetting/pagepismodesetting.h"
#include "TCMS/PISSetting/pagepissidenavi.h"
#include "TCMS/PISSetting/pagepisstationselect.h"
#include "TCMS/PISSetting/pagepisskipstationselect.h"
#include "TCMS/PISSetting/pageembroadcast.h"
#include "TCMS/PISSetting/pagesetvolume.h"
#include "TCMS/Settings/pagesettingsidenavi.h"
#include "TCMS/Settings/pagerestpcesiv.h"
#include "TCMS/Settings/pagelightsetting.h"
#include "TCMS/Events/pageeventsidenavi.h"
#include "TCMS/Events/pagecurrentevents.h"
#include "TCMS/Events/pagehisevents.h"
#include "TCMS/Maintenance/pagelogin.h"
#include "TCMS/Maintenance/pagemaintmenu.h"
#include "TCMS/Maintenance/pagetimeset.h"
#include "TCMS/Maintenance/pagewheelset.h"
#include "TCMS/Maintenance/pagetrainnumset.h"
#include "TCMS/Maintenance/pagepasswordset.h"
#include "TCMS/Maintenance/pagelcuboardswitch.h"
#include "TCMS/Maintenance/pagecleanbrake.h"
#include "TCMS/Maintenance/pagedoorparameters.h"
#include "TCMS/Maintenance/pageaccumlations.h"
#include "TCMS/Maintenance/pagespeedtest.h"
#include "TCMS/Maintenance/pagemannualtest.h"
#include "TCMS/Maintenance/pageversion.h"
#include "TCMS/Maintenance/pagemaintnetwork.h"
#include "TCMS/Maintenance/pagekeycbstat.h"
#include "TCMS/Maintenance/pageswitch.h"
#include "TCMS/Maintenance/pagemaintbattery.h"
#include "TCMS/Maintenance/pageinterface.h"
#include "TCMS/Maintenance/pagehardwarestat.h"
#include "TCMS/Maintenance/pagepisbreakertest.h"
#include "TCMS/Maintenance/pageothertests.h"
//CCTV
#ifdef TFS3536
#include "TFS3536/tfs3536lib_global.h"
#include "TFS3536/tfs3536main.h"
#else
#include "CCTV/pismonitor.h"
#endif
//Local Test
#include "LocalTest/testwidget.h"
#ifdef RUN_IN_BOARD
#include "DataCom/dataparser.h"
#endif
#ifdef M1515
#include "M1515/hisi_avic.h"
#include "M1515/hisi_dev.h"
#include "M1515/hardwaredriver.h"
#endif
//布局-位置
#define TopTitlePOS QPoint(0,0)
#define NaviBarPOS QPoint(0,1000)
#define SideNaviPOS QPoint(1095,420)
#define SideNaviPOS1 QPoint(1095,100)
#define ATCPagePOS QPoint(0,100)
#define TCMSPagePOS QPoint(0,100)
#define TCMSPagePOS2 QPoint(0,200)
#define CCTVPagePOS QPoint(1220,100)

typedef struct
{
    QWidget *pSubPage;
    QString SubPageName;
}PageInfo;

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
private slots:
    void slot_Show_Page(int pageIndex);
    void slot_Show_CCTV_Page(int pageIndex);
    void slot_Go_Back();
    void slot_Show_EventHint();
    void slot_LoginSuccess();
    //定时任务
    void slot_CycleTask();
    //数据驱动
    void slotMapVar(int comidIndex, int varoffset, unsigned long value);
    //时间设置
    void slot_TimeSet(bool xValid);
    //atc测试
    void slot_atc_test(uint8_t value);
    //PIS
    void slot_change_direction(uint8_t dir);
    void slot_set_pis_mode(int mode,bool xSet);
    void slot_PisStationSet(bool xSet);
    void slot_DepartComfirm(bool xConfirm);
    void slot_EmBroadCastOnceValid(bool xValid);
    void slot_EmBroadCastLoopValid(bool xValid);
    void slot_SkipStationActive(bool xActive);
    void slot_SkipStationID(uint32_t id);
    void slot_DepartPA(bool xTrig);
    void slot_ArrivedPA(bool xTrig);
    //加减速度测试
    void slot_speed_test_init(bool xInit);
    void slot_speed_test_start(bool xStart);
    //断路器复位
    void slot_ResetCircuitBreak(bool xTrig);
    //火灾复位
    void slot_ResetFireAlarm(bool xReset);
    //累计数据
//    void slot_DayDisSetValid(bool xValid);
    void slot_TotalDisSetValid(bool xValid);
    void slot_TotalTimeSetValid(bool xValid);
    void slot_AirComp1TimeSetValid(bool xValid);
    void slot_AirComp6TimeSetValid(bool xValid);
    void slot_ProEnergySetValid(bool xValid);
    void slot_AuxEnergySetValid(bool xValid);
    //线路号、列车号
    void slot_LineSet(bool xValid);
    void slot_TrainIDSet(bool xValid);
    //牵引辅助复位
    void slot_ResetPCE(bool xReset);
    void slot_ResetACE(bool xReset);
    //门参数
    void slot_DoorParam(uint8_t value);
    //PIS 断路器断开
    void slot_OpenPISBreaker(uint8_t index,bool xOpen);
    void slot_ResetPISBreaker(bool xReset);
    //其它测试
    void slot_AuxCompStart(bool xStart);
    void slot_SwitchToNet(bool xChange);
    void slot_NSKCutOff(bool xCutoff);
    void slot_WheelTreadClean(bool xStart);
    void slot_EDBCut(uint8_t index, bool xCut);
    void slot_HSCB_Open(bool xOpen);
    void slot_HSCB_Close(bool xClose);
    //轮经设置
    void slot_ProWheelSetValid(bool xValid);
    void slot_BrkWheelSetValid(bool xValid);
    //空调控制
    void slot_SendHvacMode(int mode,bool xValid);
    void slot_HvacTemp(int index,int temp);
    void slot_OpenNADamper(bool xOpen);
    void slot_CloseNADamper(bool xClose);
    //LCU
    void slot_ForceLCU(int index,bool xForce);
    //自检测试
    void slot_BrkManTest(bool xTest);
    //清洁制动
    void slot_CleanBrakeActive(bool xActive);
    //背光调节
    void slot_SetBackLight(int val);
    //手动切换板卡
    void slot_ManualChangeBoard(bool xChange);
private:
    Ui::MainWindow *ui;
private:
    Navigation *navigation_=nullptr;
    TopTitle *toptitle_=nullptr;
    MiniTrain *mini_train_=nullptr;
    //TCMS
    PageOperation *tcms_operation_=nullptr;//ATC page contained in TCMS operation page
    PageSpdLmtReason *tcms_speed_limit_=nullptr;
    PageEBTrigReason *tcms_eb_reason_=nullptr;
    PageBypass *tcms_bypass_=nullptr;
    PageSelfTest *tcms_self_test_=nullptr;
    PageTractionBlock *tcms_traction_block_=nullptr;
    PageHelpOperation *tcms_operation_help_=nullptr;

    PageVehicleSideNavi *tcms_vehicle_side_navi_=nullptr;
    PageTraction *tcms_traction_stat_=nullptr;
    PageBrake *tcms_brake_stat_=nullptr;
    PageAux *tcms_aux_stat_=nullptr;
    PageBattery *tcms_battery_stat_=nullptr;
    PageFire *tcms_fire_stat_=nullptr;
    PagePECU *tcms_pecu_stat_=nullptr;
    //HVAC
    PageHvac *tcms_hvac_=nullptr;
    //PIS setting
    PagePisSideNavi *tcms_pis_side_navi_=nullptr;
    PagePisModeSetting *tcms_pis_mode_set_=nullptr;
    PagePisStationSelect *tcms_pis_station_set_=nullptr;
    PagePisSkipStationSelect *tcms_pis_skip_station_set_=nullptr;
    PageEmBroadCast *tcms_pis_em_broadcast_set_=nullptr;
    PageSetVolume *tcms_pis_volume_set_=nullptr;
    //Setting
    PageSettingSideNavi *tcms_setting_side_navi_=nullptr;
    PageRestPCESIV *tcms_setting_reset_pce_siv_=nullptr;
    PageLightSetting *tcms_setting_backlight_set_=nullptr;
    //Event
    PageEventSideNavi *tcms_event_side_navi_=nullptr;
    PageCurrentEvents *tcms_event_current_=nullptr;
    PageHisEvents *tcms_event_history_=nullptr;
    //Maint
    //paramater set
    PageLogin *tcms_maint_login_=nullptr;
    PageMaintMenu *tcms_maint_menu_=nullptr;
    PageTimeSet *tcms_maint_time_set_=nullptr;
    PageWheelSet *tcms_maint_wheel_set_=nullptr;
    PageTrainNumSet *tcms_maint_trainnum_set_=nullptr;
    PagePasswordSet *tcms_maint_password_set_=nullptr;
    PageLCUBoardSwitch *tcms_maint_lcuboard_switch_=nullptr;
    PageCleanBrake *tcms_maint_clean_brake_=nullptr;
    PageDoorParameters *tcms_maint_door_para_set_=nullptr;
    //record and test
    PageAccumlations *tcms_maint_accumlations_=nullptr;
    PageSpeedTest *tcms_maint_speed_test_=nullptr;
    PageMannualTest *tcms_maint_man_self_test_=nullptr;
    PagePISBreakerTest *tcms_maint_pis_breaker_test_=nullptr;
    PageOtherTests *tcms_maint_other_test_=nullptr;
    //query
    PageMaintNetwork *tcms_maint_network_=nullptr;
    PageVersion *tcms_maint_version_=nullptr;
    PageKeyCBStat *tcms_maint_key_cbs_=nullptr;
    PageSwitch *tcms_maint_switch_=nullptr;
    PageMaintBattery *tcms_maint_battery_=nullptr;
    PageInterface *tcms_maint_io_=nullptr;
    PageHardWareStat *tcms_maint_hardware_=nullptr;
    //CCTV
#ifdef TFS3536
    TFS3536Main *tfs_cctv_=nullptr;
#else
    PISMonitor *cctv_realtime_monitor_=nullptr;
#endif
    //Comflt page
    QLabel *tcms_comflt_page1_=nullptr;
    QLabel *tcms_comflt_page2_=nullptr;
    QLabel *tcms_comflt_page3_=nullptr;
    QLabel *tcms_comflt_page4_=nullptr;
    QLabel *tcms_comflt_page5_=nullptr;
    QLabel *tcms_comflt_page6_=nullptr;
    //本地测试-强制变量界面
    TestWidget *local_test_widget_=nullptr;
    //通信Parser
#ifdef RUN_IN_BOARD
    DataParser *dataHandler=nullptr;
    HardwareDriver *hwDriver=nullptr;
#endif
protected:
    void keyPressEvent(QKeyEvent *e);
private:
    /*
     * 初始化所有界面控件
     */
    void InitWidgets();
    /*
     * 读取配置文件
     */
    void readGlobalConfigFiles();
    /*
     * 切换至指定界面，由各界面按键和导航栏按键触发
     */
    void ChangeSubPage(int oldPage, int newPage);
    int cur_subpage_;
    int old_subpage_;
    int cur_cctv_subpage_;//CCTV子系统
    /*
     * 子界面索引
     */
    QMap<int,PageInfo> map_pageindex_pageinfo_;
    /*
     * 定时任务
     */
    QTimer *cycle_task_timer_;
    //TCMS通讯故障判断
    bool tcms_comflt_=false;
    bool CheckTCMSHeartBeats();
    void Show_TCMS_ComFlt();
    void Hide_TCMS_ComFlt();
    void Hide_TCMS_ComFlt_When_CCTV_Show();
    void Show_TCMS_ComFlt_When_CCTV_Hide();
    //ATC通讯故障判断
    bool atc_comflt_=false;
    bool CheckATCHeartBeats();
    //板卡切换
    void ChangeBoard(bool should_change);
    bool manual_change_=false;
    //发送心跳
    void SendDDUHeartBeat();
    //校时
    bool has_not_sync_time_ever_;
    void SyncTCMSTime();
    void setSystemTime(uint t);
    //判断当前板卡是不是主板卡
    bool CheckCurrentBoardIsMainBoard();
    //发送版本
    void sendDDUSWVer();
    //本地测试
    void LocalTest();
    //通讯
    void startCom();
    void sendVarValue(int comidIndex,int byte,int bit,int value);
    //司机室激活
    void setPagesActive(bool xActive);
    void setPisPageActive(bool xActive);
    //故障
    void mapToEvent(int comidIndex, int varoffset, unsigned long value);
    void activeEvent(uint id, unsigned long value = 1);
    void clearEvent(uint id);
    //蜂鸣器
    void beep(bool continuously=false);
    void stopbeep();
    void setBeepEnable(bool xEnable);
    //接口检查
    void mapToInterface(int comidIndex, int index, unsigned long value);
    //字节偏移与index换算
    inline int indexOfoffset(int varoffset);
    //根据车号修改eth1 ip
    void setLocalDeviceIP_eth1(int trainnumer);
    void executeCommand(const QString &command);
    //切换至CCTV录像回放界面时
    void Show_NaviBarsAndMinitrain_When_CCTV_Hide(int page);
    void Hide_NaviBarsAndMinitrain_When_CCTV_Show(int page);
    //从程序启动之后开始计数每妙+1
    int ddu_start_cnt = 0;
};
#endif // MAINWINDOW_H
